<?php
!defined('P_W') && exit('Forbidden');
class PW_PushDataService {
	var $_count = 0;
	function getPush($id) {
		$pushDataDB = $this->_getPushDataDB();
		return $pushDataDB->get($id);
	}
	function getModelPushs($hid,$model,$page,$preg=10) {
		$page = (int) $page;
		if ($page<1) $page = 1;
		$pushDataDB = $this->_getPushDataDB();
		$start = ($page-1) * $preg;
		$temp = $pushDataDB->getByModel($hid,$model,$start,$preg);
		foreach ($temp as $key=>$value) {
			$temp[$key] = $this->_unDealPushData($value);
		}

		return $temp;
	}
	function getOpenPushs($hid,$model,$num) {
		$pushDataDB = $this->_getPushDataDB();
		$temp = $pushDataDB->getOpenPushs($hid,$model,$num);
		foreach ($temp as $key=>$value) {
			$temp[$key] = $this->_unDealPushData($value);
		}

		return $temp;
	}
	function addPushData($hid,$model,$array) {
		$pushDataDB = $this->_getPushDataDB();
		$array = $this->_dealPushData($array,$hid,$model);
		$temp = $pushDataDB->insert($array);
		$this->_updateCache($hid,$model);
		return $temp;
	}
	
	function editPushData($id,$array) {
		$pushDataDB = $this->_getPushDataDB();
		$array = $this->_dealPushData($array);
		$pushDataDB->update($id,$array);
		$temp = $this->getPush($id);
		if ($temp) {
			$this->_updateCache($temp['hid'],$temp['model']);
		}
	}
	function deletePushData($id) {
		$pushDataDB = $this->_getPushDataDB();
		$temp = $pushDataDB->get($id);
		if (!$temp) return false;
		$pushDataDB->delete($id);
		$this->_updateCache($temp['hid'],$temp['model']);
	}
	function deletePushDatas($array) {
		if (!$array || !is_array($array)) return false;
		foreach ($array as $key=>$value) {
			$this->deletePushData($value);
		}
	}
	function setOpens($array) {
		$this->_changeIffailureState($array,0);
	}
	function setCloses($array) {
		$this->_changeIffailureState($array,1);
	}
	function _changeIffailureState($array,$state) {
		if (!$array || !is_array($array)) return false;
		$pushDataDB = $this->_getPushDataDB();
		$pushDataDB->setIffailureStates($array,$state);
		$id = end($array);
		$temp = $pushDataDB->get($id);
		if (!$temp) return false;
		$this->_updateCache($temp['hid'],$temp['model']);
	}
	
	function getPushDataPages($hid,$model,$page,$url,$preg=10) {
		$page = (int) $page;
		if ($page<1) $page = 1;
		$total = $this->_getCount($hid,$model);
		$numofpage = ceil($total/$preg);
		$numofpage < 1 && $numofpage = 1;
		$page > $numofpage && $page = $numofpage;

		return numofpage($total,$page,$numofpage,$url);
	}
	function _updateCache($hid,$model) {
		$pageCache = getPageCache('info',$hid);
		$temp_config = $pageCache->config;
		foreach ($temp_config as $key=>$value) {
			if (isset($value['model']) && $value['model']==$model) {
				$pageCache->deleteCache($key);
			}
		}
	}
	function _getCount($hid,$model) {
		$pushDataDB = $this->_getPushDataDB();
		return $pushDataDB->countByModel($hid,$model);
	}
	
	function _dealPushData($array,$hid=false,$model=false) {
		global $timestamp;
		if ($hid) $array['hid'] = (int)$hid;
		if ($model) $array['model'] = $model;
		if (!$array['pushtime']) $array['pushtime'] = $timestamp;
		$array['url'] = str_replace('&#61;','=',$array['url']);
		$array['titlecss']	= $this->_titleCss($array['titlecss']);
		$array['offset']	= (int) $array['offset'];
		return $array;
	}
	function _unDealPushData($array) {
		$array['pushtime'] = get_date($array['pushtime'],'Y-m-d');
		$array = $this->_colorTitle($array);
		return $array;
	}
	function _titleCss($css) {
		return array(
			'color' => $css['color'],
			'b' => $css['b'],
			'i' => $css['i'],
			'u' => $css['u']
		);
	}
	function _colorTitle($rt) {
		global $timestamp;
		if ($rt['titlecss'] && $rt['title']) {
			if ($rt['titlecss']['color']) $rt['title'] = "<font color=".$rt['titlecss']['color'].">".$rt['title']."</font>";
			if ($rt['titlecss']['b']) $rt['title'] = "<b>".$rt['title']."</b>";
			if ($rt['titlecss']['i']) $rt['title'] = "<i>".$rt['title']."</i>";
			if ($rt['titlecss']['u']) $rt['title'] = "<u>".$rt['title']."</u>";
		}
		return $rt;
	}
	function _getPushDataDB() {
		return House::loadDB('house_pushdata');
	}
}